<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      //获取系统当前时间
      var nowdate = new Date();
      var y = nowdate.getFullYear();
      var m = nowdate.getMonth() + 1;
      var d = nowdate.getDate();
      var formatnowdate = y + "-" + m + "-" + d;
      console.log(formatnowdate);
      //获取过去一周的时间
      var myDate = new Date();
      var weekDate = new Date(myDate.getTime() - 7 * 24 * 3600 * 1000); // 计算开始时间用
      var weekDate2 = new Date(myDate.getTime() - 7 * 24 * 3600 * 1000); // 计算结束时间用

      var day = weekDate.getDay();
      var time = weekDate.getDate() - day + (day === 0 ? -6 : 1);

      var startDate = new Date(weekDate.setDate(time));
      var beginTime =
        startDate.getFullYear() +
        "-" +
        (startDate.getMonth() + 1) +
        "-" +
        startDate.getDate();
      var endDate = new Date(weekDate2.setDate(time + 6));
      var endTime =
        endDate.getFullYear() +
        "-" +
        (endDate.getMonth() + 1) +
        "-" +
        endDate.getDate();

      //获取系统前一个月的时间
      nowdate.setMonth(nowdate.getMonth() - 1);
      var y = nowdate.getFullYear();
      var m = nowdate.getMonth() + 1;
      var d = nowdate.getDate();
      var formatwdate = y + "-" + m + "-" + d;
      console.log(formatwdate);
      console.log(getPreMonth("2020-10-31"));
      function getPreMonth(date) {
        var arr = date.split("-");
        var year = arr[0]; //获取当前日期的年份
        var month = arr[1]; //获取当前日期的月份
        var day = arr[2]; //获取当前日期的日
        var days = new Date(year, month, 0);
        days = days.getDate(); //获取当前日期中月的天数
        var year2 = year;
        var month2 = parseInt(month) - 1;
        if (month2 == 0) {
          year2 = parseInt(year2) - 1;
          month2 = 12;
        }
        var day2 = day;
        var days2 = new Date(year2, month2, 0);
        days2 = days2.getDate();
        if (day2 > days2) {
          day2 = days2;
        }
        if (month2 < 10) {
          month2 = "0" + month2;
        }
        var t2 = year2 + "-" + month2 + "-" + day2;
        return t2;
      }
      function getLastMonthTime(date) {
        //  1    2    3    4    5    6    7    8    9   10    11   12月
        var daysInMonth = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
        var strYear = date.getFullYear();
        var strDay = date.getDate();
        var strMonth = date.getMonth() + 1;
        //一、解决闰年平年的二月份天数   //平年28天、闰年29天//能被4整除且不能被100整除的为闰年,或能被100整除且能被400整除
        if ((strYear % 4 === 0 && strYear % 100 !== 0) || strYear % 400 === 0) {
          daysInMonth[2] = 29;
        }
        if (strMonth - 1 === 0) {
          //二、解决跨年问题
          strYear -= 1;
          strMonth = 12;
        } else {
          strMonth -= 1;
        }
        //  strDay = daysInMonth[strMonth] >= strDay ? strDay : daysInMonth[strMonth];
        strDay = Math.min(strDay, daysInMonth[strMonth]); //三、前一个月日期不一定和今天同一号，例如3.31的前一个月日期是2.28；9.30前一个月日期是8.30

        if (strMonth < 10) {
          //给个位数的月、日补零
          strMonth = "0" + strMonth;
        }
        if (strDay < 10) {
          strDay = "0" + strDay;
        }
        var datastr = strYear + "-" + strMonth + "-" + strDay;
        return datastr;
      }
    </script>
  </body>
</html>
